export default function ScrollDown(el) {
  var that = this
  this.hasMoreData = [] //是否到达最后一页
  this.canFetch = [] //是否允许发起请求.在请求发起前一刻设为false,请求处理完毕后,设为true.用来防止短时间内多次向上滚动时,不停的重复ajax请求
  this.nextPageIdx = []
  this.eachPageNum = 10 //每次载入文章条数
  this.bottomRange = 60 //距离底部xx像素进行加载
  this.fn = []

  var __ele = el === window ? [window] : Array.from(document.getElementsByClassName(el))
  __ele.forEach((v, k) => {
    that.hasMoreData[k] = true
    that.canFetch[k] = true
    that.nextPageIdx[k] = 0
    that.fn[k] = function () { }

    v.addEventListener('scroll', function () {
      var domScrollHeight
      if (v === window) {
        v = document.documentElement
      }
      domScrollHeight = v.scrollHeight
      //用v.clientHeight而不是v.offsetHeight的原因是:document.documentElement的offsetHeight等于scrollHeight,但其他dom元素又不是这样
      if ((v.scrollTop + v.clientHeight > domScrollHeight - that.bottomRange) && that.canFetch[k]) {
        if (that.hasMoreData[k]) {
          typeof that.fn[k] === 'function' && that.fn[k](k)
        }
      }
    })
  })
}